//
//  GoodsDetailViewController.m
//  EasyToo
//
//  Created by zhengzhe on 13-9-14.
//
//

#import "GoodsDetailViewController.h"
#import "UIImageView+OnlineImage.h"
#import "ShoppingManager.h"
#import "OrderInfo.h"
#import "UPPayPlugin.h"
#import "ShoppingOrdersViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface GoodsDetailViewController ()

@end

@implementation GoodsDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.navigationItem.title = @"商品详情";
    [self.view setBackgroundColor:BackgroundColor];
    
    [self initScrollview];

    [self initView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    RelaseAndNil(_numTF);
    RelaseAndNil(_confirmBtn);
    [super dealloc];
}

- (void)initScrollview
{
    int width = [DeviceUtil getScreenWidth];
    int heigth = self.view.frame.size.height;
    
    _bgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, width, heigth)];
    _bgScrollView.directionalLockEnabled = YES; //只能一个方向滑动
    _bgScrollView.pagingEnabled = NO; //是否翻页
    _bgScrollView.backgroundColor = [UIColor clearColor];
    _bgScrollView.showsVerticalScrollIndicator =YES; //垂直方向的滚动指示
    _bgScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;//滚动指示的风格
    _bgScrollView.showsHorizontalScrollIndicator = NO;//水平方向的滚动指示
    [_bgScrollView setContentSize:CGSizeMake(width, 1000)];
    
    [self.view addSubview:_bgScrollView];
    
    UIControl *control = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, _bgScrollView.frame.size.width, _bgScrollView.frame.size.height)];
    [control addTarget:self action:@selector(onBackgroundClick) forControlEvents:UIControlEventTouchUpInside];
    [_bgScrollView addSubview:control];
}

- (void)initView
{
//    UIScrollView *bgScrollView = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)] autorelease];
    
//    [self.view addSubview:bgScrollView];
    
    UIImageView *goodsImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(50, 20, [DeviceUtil getScreenWidth] - 100, [DeviceUtil getScreenWidth] - 100)] autorelease];
    
    goodsImageView.backgroundColor = [UIColor whiteColor];
    [_bgScrollView addSubview:goodsImageView];
//    [bgScrollView addSubview:goodsImageView];
    
    UIImageView *markImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 90, 30)] autorelease];
    markImageView.image = [UIImage imageNamed:@"goods_mark"];
    [goodsImageView addSubview:markImageView];
    
    UILabel *priceLabel = [[[UILabel alloc] initWithFrame:CGRectMake(8, 5, 80, 20)] autorelease];
    priceLabel.backgroundColor = [UIColor clearColor];
    priceLabel.textColor = [UIColor whiteColor];
    priceLabel.font = [UIFont boldSystemFontOfSize:15];
    
    [markImageView addSubview:priceLabel];
    
    UILabel *nameLabel = [[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(goodsImageView.frame), CGRectGetMaxY(goodsImageView.frame)+ 15, goodsImageView.bounds.size.width, 40)] autorelease];
    nameLabel.font = [UIFont systemFontOfSize:16];
    nameLabel.numberOfLines = 0;
    nameLabel.backgroundColor = [UIColor clearColor];
    [_bgScrollView addSubview:nameLabel];
    
    //数据
    [goodsImageView setOnlineImage:self.goodsInfo.imgUrl];
    priceLabel.text = [NSString stringWithFormat:@"￥%.2f", self.goodsInfo.salePrice];
    NSString *nameText = self.goodsInfo.name;
    
    //根据text的大小重设label的frame
    CGSize maxSize = CGSizeMake(nameLabel.bounds.size.width, 300);
    CGSize labelSize = [nameText sizeWithFont:nameLabel.font constrainedToSize:maxSize lineBreakMode:NSLineBreakByTruncatingTail];
    nameLabel.frame = CGRectMake(nameLabel.frame.origin.x, nameLabel.frame.origin.y, labelSize.width, labelSize.height);
    nameLabel.center = CGPointMake([DeviceUtil getScreenWidth] / 2, nameLabel.center.y);
    nameLabel.text = nameText;
    
    UILabel *numLabel = [[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(nameLabel.frame), CGRectGetMaxY(nameLabel.frame) + 20, 80, 25)] autorelease];;
    [numLabel setText:@"选择数量"];
    [numLabel setBackgroundColor:[UIColor clearColor]];
    [_bgScrollView addSubview: numLabel];
    
    _numTF = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(numLabel.frame) + 10, CGRectGetMaxY(nameLabel.frame) + 16, 80, 30)];
    [_numTF setBackgroundColor:[UIColor whiteColor]];
    _numTF.keyboardType = UIKeyboardTypeNumberPad;
    _numTF.font = [UIFont boldSystemFontOfSize:20];
    _numTF.borderStyle = UITextBorderStyleRoundedRect;
    _numTF.layer.borderColor = [UIColor grayColor].CGColor;
    _numTF.layer.borderWidth = 1 ;
    _numTF.layer.cornerRadius = 10 ;
    _numTF.returnKeyType =UIReturnKeyDone;
    _numTF.textAlignment = NSTextAlignmentCenter;
    _numTF.delegate = self;
    _numTF.text = @"1";
    [_bgScrollView addSubview:_numTF];
    
    
    _confirmBtn = [[UIButton alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(_numTF.frame) + 20, [DeviceUtil getScreenWidth] - 40, 40)];
    
    [_confirmBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [_confirmBtn setBackgroundImage:[UIImage imageNamed:@"xyb"] forState:UIControlStateNormal];
    [_confirmBtn addTarget:self action:@selector(onConfirm) forControlEvents:UIControlEventTouchUpInside];
    [_bgScrollView addSubview:_confirmBtn];
    
}


- (void)onConfirm
{
    OrderInfo *order = [[[OrderInfo alloc] init] autorelease];
    order.goodsId = self.goodsInfo.goodsId;
    order.price = self.goodsInfo.salePrice;
    order.name = self.goodsInfo.name;
    order.num = [_numTF.text intValue];
    order.goodsInfo = self.goodsInfo;

    ShoppingOrdersViewController *shoppingOrder = [[[ShoppingOrdersViewController alloc] init] autorelease];
    shoppingOrder.ordersArray = [NSMutableArray arrayWithObject:order];
    
    [self.navigationController pushViewController:shoppingOrder animated:YES];
}

//- (void)onConfirm
//{
////    [UPPayPlugin startPay:@"201309151655210098102" sysProvide:nil spId:nil mode:@"01" viewController:self delegate:self];
//    
//}

-(void)UPPayPluginResult:(NSString*)result
{
    NSLog(@"===%@===", result);
}

#pragma mark UITextFieldDelegate

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    int orderNum = [_numTF.text intValue];
    
    _confirmBtn.enabled = (orderNum > 0 && orderNum < 99)? YES : NO;
    
    
}

//开始编辑输入框的时候，软键盘出现，执行此事件
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    textField.text = @"";
    CGRect frame = textField.frame;
    int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216.0);//键盘高度216
    
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    
    //将视图的Y坐标向上移动offset个单位，以使下面腾出地方用于软键盘的显示
    if(offset > 0)
        self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
    
    [UIView commitAnimations];
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//    [_creditnumTF resignFirstResponder];
//    [_nameTF resignFirstResponder];
//    [_cashTF resignFirstResponder];
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//    [textField resignFirstResponder];
    return YES;
}

- (void)onBackgroundClick
{
    [_numTF resignFirstResponder];
    
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    
    self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    
    [UIView commitAnimations];

    
}

@end
